import { valueList, valueUpdate, valueRemove } from '@/services/dict';

const defaultModal = {
  visible: false,
  data: {},
};

export default {
  namespace: 'dictValue',

  state: {
    index: {},
    list: [],
    modal: { ...defaultModal },
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const { data } = yield call(valueList, payload);
      yield put({
        type: 'list',
        payload: data,
      });
    },
    *update({ payload }, { call }) {
      return yield call(valueUpdate, payload);
    },
    *openModal({ payload }, { put }) {
      yield put({
        type: 'modal',
        payload: {
          data: payload || {},
          visible: true,
        },
      });
    },
    *closeModal(_, { put }) {
      yield put({
        type: 'modal',
        payload: { ...defaultModal },
      });
    },
    *remove({ payload }, { call }) {
      yield call(valueRemove, payload);
    },
  },

  reducers: {
    index(state, { payload }) {
      return { ...state, index: payload };
    },
    list(state, { payload }) {
      return { ...state, list: payload };
    },
    modal(state, { payload }) {
      return { ...state, modal: { ...state.modal, ...payload } };
    },
  },
};
